package com.klhappy.houyuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.bean.EvalationItem;
import com.klhappy.houyuan.bean.Images;
import com.klhappy.houyuan.bean.MoreEvalustion.Records;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.utils.imageselector.PictureDialog;
import com.klhappy.houyuan.view.CircleImageView;
import com.klhappy.houyuan.view.ImageManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by MrPu on 15/12/17.
 */
public class LookMoreAdapter extends BaseAdapter {
    private Context mContext;
    private List<Records> records;

    public LookMoreAdapter(Context context, List<Records> records) {
        this.mContext = context;
        this.records = records;
    }

    public void setNotif(List<Records> records){
        this.records = records;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return records.size();
    }

    @Override
    public Object getItem(int position) {
        return records.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.evalation_item, null);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        List<Images> images = JsonCode.getClassList(records.get(position).getCommentImages(), Images.class);
        ImageManager.loadImage(UrlConstant.BASE_URL + records.get(position).getOwn_icon(), holder.con);
        holder.userName.setText(records.get(position).getOwn_nickname());
        holder.content.setText(records.get(position).getCo_cont());
        holder.time.setText(records.get(position).getCo_date());
        holder.gv_img.setAdapter(new ImageAdapter(mContext, images));
        holder.gv_img.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int j, long id) {
                List<String> img = new ArrayList<String>();
                List<Images> imgs = JsonCode.getClassList(records.get(position).getCommentImages(), Images.class);
                for (int i = 0; i < imgs.size(); i++){
                    img.add(UrlConstant.BASE_URL + imgs.get(i).getImagePath());
                }
                PictureDialog dialog = new PictureDialog();
                dialog.setCurItem(j);
                dialog.setOnView(mContext, img);
                dialog.show();
            }
        });
        return convertView;
    }

    public static class Holder {
        @InjectView(R.id.iv_icon)
        CircleImageView con;
        @InjectView(R.id.tv_UserName)
        TextView userName;
        @InjectView(R.id.tv_Content)
        TextView content;
        @InjectView(R.id.tv_time)
        TextView time;
        @InjectView(R.id.gv_img)
        GridView gv_img;

        public Holder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
